/**
 * @author caslyn
 */
$(document).ready(function(){
//	$("#dialog_newfeedback").dialog({
//		bgiframe: true,
//		autoOpen: false,
//		width: 490,
//		modal: true,
//		buttons: {
//			'Save': function() {
//				if(InvalidNewFeedback()){
//					exit;
//				}
//				temp = $('#new_feedback_content').val();
//				temp = temp.replace(/\n/g,"<br>");
//				$.getJSON("json.php", { "task": "addNewFeedback",
//				"pageid":$('#pageid').val(), "subject": $('#new_feedback_subject').val(),
//				"content": temp, "referenceid": $('#referenceid').val() } , 
//					function(data){
//						$('#feedbacklist').prepend('<span id="spnfeedback' + data.id + '"><b>' + data.subject + 
//						'</b><br/><span>' +
//						data.content + '</span> ' +
//						WriteActionButton("Edit", "editFeedback(" + data.id + ")") + ' ' +
//						WriteActionButton("Delete", "deleteFeedback(" + data.id + ")") +
//						'<hr/></span>' );
////							alert('Save success. ');
//						$("#dialog_newfeedback").dialog('close');
//					}
//				);
////					$(this).dialog('close');
//			},
//			Cancel: function() {
//				$(this).dialog('close');
//			}
//		},
//		close: function() {
//			$('#new_feedback_subject').val('');
//			$('#new_feedback_content').val('');
//		}
//	});

	$("#dialog_editfeedback").dialog({
		bgiframe: true,
		autoOpen: false,
		width: 490,
		modal: true,
		buttons: {
			'Save': function() {
				if(InvalidFeedback()){exit;}
				temp = $('#edit_feedback_content').val();
				temp = temp.replace(/\n/g,"<br>");
				$.getJSON("json.php", { "task": "saveFeedback", "id": $('#edit_feedback_id').val(),
				"pageid": $('#pageid').val(), "subject": $('#edit_feedback_subject').val(),
				"content": temp } , 
					function(data){
//							spanname = '#spnfeedback' + data.id;
						$('#spnfeedback' + data.id).children('span').html(data.content);
						$('#spnfeedback' + data.id).children('b').html(data.subject);
						alert('Save success. ');
						$("#dialog_editfeedback").dialog('close');
					}
				);
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			$('#edit_feedback_subject').val('');
			$('#edit_feedback_content').val('');
		}
	});
	
//	$('#addNewFeedback').click(function() {
//		$('#dialog_newfeedback').dialog('open');
//	});
	
});

function editFeedback(nid){
	// load data from json
//	alert('Editing feedback '+ id);
		temp = $('#spnfeedback'+nid).children('span').html();
		temp = temp.replace(/<br>/g, "\n");
		$('#edit_feedback_subject').val($('#spnfeedback'+nid).children('b').html());
		$('#edit_feedback_content').val(temp);
		$('#edit_feedback_id').val(nid);
		// open edit dialog
		$('#dialog_editfeedback').dialog('open');
}

function deleteFeedback(feedbackid){
	// ask user if want to delete
	var answer = confirm("Do you really want to delete this feedback?");
	if(answer == true){
	// perform ajax  request
		$.post("json.php",{task:"deleteFeedback",id:feedbackid},
		function(data){
			if(data=="1"){
				alert("Feedback deleted.");
				$('#spnfeedback'+feedbackid).remove();
			}
		});			
	}
	// show success.
}

function InvalidNewFeedback(){
	if ( $('#newfeedbackcontent').val().length < 1 ){ alert('Content cannot be empty.'); return true; };
	if ( $('#pageid').val().length < 1 ){ alert('Page ID cannot be empty.'); return true; };
	if ( $('#newfeedbacksubject').val().length < 1 ){ alert('Subject cannot be empty.'); return true; };
	if ( $('#referenceid').val().length < 1 ){ alert('Reference ID cannot be empty.'); return true; };
	return false;
}

function InvalidFeedback(){
	if ( $('#edit_feedback_id').val().length < 1 ){ alert('Feedback ID cannot be empty.'); return true; };
	if ( $('#edit_feedback_content').val().length < 1 ){ alert('Content cannot be empty.'); return true; };
	if ( $('#pageid').val().length < 1 ){ alert('Page ID cannot be empty.'); return true; };
	if ( $('#edit_feedback_subject').val().length < 1 ){ alert('Subject cannot be empty.'); return true; };
	return false;
	
}

function postFeedback(){
	if(InvalidNewFeedback()){
		exit;
	}
	temp = $('#newfeedbackcontent').val();
	temp = temp.replace(/\n/g,"<br>");
	$.getJSON("json.php", { "task": "addNewFeedback",
	"pageid":$('#pageid').val(), "subject": $('#newfeedbacksubject').val(),
	"content": temp, "referenceid": $('#referenceid').val() } , 
		function(data){
			$('#feedbacklist').prepend('<span id="spnfeedback' + data.id + '"><b>' + data.subject + 
			'</b><br/><span>' +
			data.content + '</span> ' +
			WriteActionButton("Edit", "editFeedback(" + data.id + ")") + ' ' +
			WriteActionButton("Delete", "deleteFeedback(" + data.id + ")") +
			'<hr/></span>' );
		}
	);
	$('#newfeedbackcontent').val('');
	$('#newfeedbacksubject').val('');
}
	

